/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fir;

import javax.swing.*;
import java.awt.*;
import java.awt.Color;

/**
 *
 * @author zhangyucen
 */
public class ChessBoard extends JPanel {

    Image boardpic, whitepoint, blackpoint;
    final static int lineBeginX = 32, lineBeginY = 32, lineWidth = 30, wDiameter = 28, bDiameter = 30;
    Chess chess;

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        //draw the board
        g.drawImage(boardpic, 5, 5, 480, 473, this);
        g.setColor(Color.black);
        g.drawRect(lineBeginX, lineBeginY, 14 * lineWidth, 14 * lineWidth);
        for (int i = 1; i < 14; i++) {
            g.drawLine(lineBeginX + lineWidth * i, lineBeginY, lineBeginX + lineWidth * i, lineBeginY + 14 * lineWidth);
        }
        for (int i = 1; i < 14; i++) {
            g.drawLine(lineBeginX, lineBeginY + lineWidth * i, lineBeginX + 14 * lineWidth, lineBeginY + lineWidth * i);
        }
        g.fillRect(lineBeginX + 3 * lineWidth - 2, lineBeginY + 3 * lineWidth - 2, 5, 5);
        g.fillRect(lineBeginX + 11 * lineWidth - 2, lineBeginY + 3 * lineWidth - 2, 5, 5);
        g.fillRect(lineBeginX + 3 * lineWidth - 2, lineBeginY + 11 * lineWidth - 2, 5, 5);
        g.fillRect(lineBeginX + 11 * lineWidth - 2, lineBeginY + 11 * lineWidth - 2, 5, 5);
        g.fillRect(lineBeginX + 7 * lineWidth - 2, lineBeginY + 7 * lineWidth - 2, 5, 5);
//        draw the chess
        //g.drawImage(whitepoint, lineBeginX + 5 * lineWidth - wDiameter / 2, lineBeginY + 4 * lineWidth - wDiameter / 2, this);
        //g.drawImage(blackpoint, lineBeginX + 5 * lineWidth - bDiameter / 2, lineBeginY + 5 * lineWidth - bDiameter / 2, this);
        for (int i = 0; i < 15; i++) {
            for (int j = 0; j < 15; j++) {
                if (chess.point[i][j] == 1) //black chess
                {
                    g.drawImage(blackpoint, lineBeginX + j * lineWidth - bDiameter / 2, lineBeginY + i * lineWidth - bDiameter / 2, this);
                }
                if (chess.point[i][j] == 2) {
                    g.drawImage(whitepoint, lineBeginX + j * lineWidth - wDiameter / 2, lineBeginY + i * lineWidth - wDiameter / 2, this);
                }
            }
        }
    }

    public void draw() {
        repaint();
    }

    ChessBoard() {
        super();
        chess = new Chess();
        setLayout(null);
        boardpic = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("images/wood/board.jpg"));
        whitepoint = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("images/wood/white.png"));
        blackpoint = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("images/wood/black.png"));
    }
}
